package com.woniuxy.hrms.dao.impl;

import com.woniuxy.hrms.dao.IHrDao;
import com.woniuxy.hrms.entity.Hr;
import com.woniuxy.hrms.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @program: hrms
 * @description:
 * @create: 2024-08-09 11:53
 **/
public class IHrDaoImpl implements IHrDao {
    Connection connection;
    PreparedStatement ps;
    ResultSet rs;
    String sql;

    @Override
    public int add(Hr hr) throws SQLException {
        connection= DBUtil.getCon();
        sql="insert into hr(real_name,phone,username,password) values(?,?,?,?)";
        ps=connection.prepareStatement(sql);
        ps.setString(1,hr.getName());
        ps.setString(2, hr.getPhone());
        ps.setString(3, hr.getUsername());
        ps.setString(4, hr.getPassword());
        int i=ps.executeUpdate();
        DBUtil.release(connection,ps,rs);
        return i;
    }

    @Override
    public int delete(int id) throws SQLException {
        return 0;
    }

    @Override
    public int update(Hr hr) throws SQLException {
        return 0;
    }


    @Override
    public List<Hr> queryALL() throws SQLException {
        return null;
    }

    @Override
    public boolean queryByUsername(String username) throws SQLException {
        connection=DBUtil.getCon();
        sql="select * from hr where username=?";
        ps=connection.prepareStatement(sql);
        ps.setString(1,username);
        rs=ps.executeQuery();//执行查询并返回一个resultset对象，有一个指针指向到第一条数据之前
        boolean next=rs.next();
        DBUtil.release(connection,ps,rs);
        return next;
    }
}
